import config from '../config/public'

function request({
  method,
  url,
  data
}) {
  const Authorization = uni.getStorageSync('token') || 'eyJhbGciOiJIUzUxMiJ9.eyJsb2dpbl91c2VyX2tleSI6ImExN2RkMjMxLTVmMjctNDllZC1iNWQwLWNmZDlmMDdkOWViMyJ9.ba__IOq2MTHfiWMzxAXLfbv7k4z8YLnJHcl5VujNs-tgoqDI-OVZVGM-3_KIOiiQwkcMKFc47eMfLIh4UR6brA'
  return new Promise((resolve, reject) => {
    // uni.showLoading({
    // 	title: '加载中'
    // });
    uni.request({
      url: config.beaeUrl + url,
      data,
      header: {
        Authorization
      },
      method,
      success(res) {
        // uni.hideLoading();
        switch (res.data.code) {
          case 200:
            // if (res.data.code == 1) {
            resolve(res.data)
            // } else if (res.data.code == 3) {
            //   uni.showToast({
            //     title: res.data.msg,
            //     icon: 'none',
            //     duration: 1500
            //   })
            //   resolve(res.data)
            // } else {
            //   reject(res.data)
            //   uni.showToast({
            //     title: res.data.msg,
            //     icon: 'none',
            //     duration: 1500
            //   })
            // }
            break;
          case 401:
            uni.showToast({
              title: '请先登录',
              duration: 500,
              icon: 'none',
              mask: true,
              success: () => {
                uni.clearStorage()
                setTimeout(() => {
                  uni.reLaunch({
                    url: '/pages/my/login'
                  })
                }, 500)
              },
              /* 
               userInfoStore
               */
            })
            break;
          case 404:
            uni.showToast({
              title: '服务器错误',
              duration: 1500,
              icon: 'none'
            })
            break;
          case 500:
            uni.showToast({
              title: res.data.msg,
              duration: 2000,
              icon: 'none'
            })
        }
      },
      fail(err) {
        uni.hideLoading();
        uni.showToast({
          title: err,
          duration: 1500,
          icon: 'none'
        })
        resolve(false)
      }
    })
  })
}

export default request;